<?php
include 'debut.php';
?>

<body>

<?php
	include 'menu.php';
	echo $_SESSION['admin'];
	$d = dir("./img/photos");
	$tab=array();
	while (false !== ($entry = $d->read())) {
		if(substr($entry,0,1)!="."){
			$tab[]=$entry;
		}
	}
	$d->close();
	
	if(isset($_FILES['datafile']))
	{ 
		$desc=$_POST['desc'];
		$title=$_POST['title'];
		 $dossier = 'img/photos/';
		 $fichier = basename($_FILES['datafile']['name']);
		 $extension=end(explode(".", $fichier));
		 if(move_uploaded_file($_FILES['datafile']['tmp_name'], $dossier . $fichier)) //Si la fonction renvoie TRUE, c'est que ça a fonctionné...
		 {
		 ?>
		 <script>$(".alert").alert()</script>
		 <div class="alert alert-succes">  
		  <a class="close" data-dismiss="alert">×</a>  
		  Upload effectué avec succès !
		</div>
		<?php
			  $con->query("INSERT INTO photos VALUES ('','$title','$desc','djoa','$extension')");
			  $id=$con->insert_id;
			  rename("./img/photos/".$fichier."","./img/photos/".$id.".".$extension.""); 
		 }
		 else //Sinon (la fonction renvoie FALSE).
		 {
			  echo 'Echec de l\'upload !';
		 }
	}
?>

<ul class="nav nav-tabs">
  <li class="active"><a href="#">Djoa</a></li>
  <li><a href="#">Michel Ilhat</a></li>
</ul>
<div class="container photo">
	<ul class="pagination">		
		<?php
		if(isset($_GET['p'])){
			$active=$_GET['p'];
		}else{
			$active=1;
		}
		
		if($active==1){
			echo "<li class='disabled'><a href='?p=1'>&laquo;</a></li>";
		}else{
			echo "<li><a href='?p=1'>&laquo;</a></li>";
		}
		$end=ceil(count($tab)/9);
		for($i=1;$i<=$end;$i++){
			if($active==$i){
				echo "<li class='active'><a href='?p=".$i."'>".$i."<span class='sr-only'>(current)</span></a></li>";
			}else{
				echo "<li><a href='?p=".$i."'>".$i."</a></li>";
			}
			
		}
		if($active==$end){
			echo "<li class='disabled'><a href='?p=".$end."'>&raquo;</a></li>";
		}else{
			echo "<li><a href='?p=".$end."'>&raquo;</a></li>";
		}
		?>
	</ul>
<?php
	for($i=0;$i<3;$i++){
?>
	<div class="row">
	<?php
	if($i==0 and $_SESSION['admin']){
	?>
		<div class="col-sm-6 col-md-4">
		<a href='#addPhotos' class='thumbnail' data-toggle='modal' >
						<i class='fa fa-plus-circle fa-16x'></i></a>
		</div>
	<?php
		$k=1;
	}else{
		$k=0;
	}
	?>
<?php	
	for($j=$k;$j<3;$j++){
		$numPhoto=$i*3+$j+(($active-1)*9);
		if(count($tab)<=$numPhoto){
			break;
		}
		$chemin="./img/photos/".$tab[$numPhoto];

		 echo "<div class='col-sm-6 col-md-4'>
			<a href='#myModal' class='thumbnail' data-toggle='modal' >
			  <img  src='$chemin' alt='pots' onClick='javascript:setChemin(\"".$chemin."\",\"".$tab[$numPhoto]."\")'>
			</a>
		  </div>";
		  
	 }
	 ?>
	</div>
	
<?php
}
?>
<div class="modal fade" id="myModal">
<input type='hidden' id='idimg' value=''/>
  <div class="modal-dialog">
    <div id='modalPhoto' class="modal-content">
      
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<div class="modal fade" id="addPhotos">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Ajouter une photo</h4>
      </div>
      <div class="modal-body">
		<form id="form" action="photos.php" class="form-horizontal" method="post" enctype="multipart/form-data">
		  <div class="control-group">
			<label class="control-label" for="title">Titre</label>
			<div class="controls">
			  <input type="text" id="title" name="title" placeholder="Titre">
			</div>
		  </div>
		  <div class="control-group">
			<label class="control-label" for="desc">Description</label>
			<div class="controls">
			  <textarea id="desc" rows="3" name="desc"></textarea>
			</div>
		  </div>
		  <div class="control-group">
			<label class="control-label" for="chemin">Chemin</label>
			<div class="controls">
				<input type="file" id="chemin" name="datafile" size="40">
			</div>
		  </div>
		</form>
	</div>
	
      <div class="modal-footer">
		<a href="#" class="btn">Annuler</a>
		<a href="#" onclick="document.getElementById('form').submit()" class="btn btn-primary">Ajouter</a>
	  </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</div>
</body>
</html>